package com.clkj.xiuzheng.service;

import com.clkj.xiuzheng.dao.*;
import com.clkj.xiuzheng.pojo.OutStock;
import com.clkj.xiuzheng.pojo.OutStockItem;
import com.clkj.xiuzheng.pojo.Product;
import com.clkj.xiuzheng.pojo.ProductStock;
import com.clkj.xiuzheng.pojo.common.DateFormatHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yawang
 * @create 2024/04/18 15:52:53
 **/
@Service
public class OutStockServiceImpl implements OutStockService{
    @Autowired
    private OutStockMapper outStockMapper;
    @Autowired
    private OutStockItemMapper outStockItemMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private WarehouseMapper warehouseMapper;

    private Map map;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductStockMapper productStockMapper;

    @Override
    @Transactional(rollbackFor = Exception.class, noRollbackFor = RuntimeException.class)
    public Map addOutStock(OutStock outStock) {
        Integer id = maxId();
        if (id == null) {
            id = 1;
        } else {
            id += 1;
        }
        String s = String.format("%08d", id);
        String DateTime = DateFormatHelper.currentTimes();
        outStock.setOutDate(DateTime);
        outStock.setOutNumber("XZYY" + s);
        outStock.setApplyStatus(0);
        outStockMapper.addOutStock(outStock);
        List<OutStockItem> itemList = outStock.getItemList();
        itemList.forEach(item -> {
            item.setOutId(outStock.getOutId());
            item.setItemFlag(1);
            item.setProductTotal(item.getProductPrice()*item.getProductNum());
            item.setItemDate(DateTime);
        });
        outStockItemMapper.insertList(itemList);
        for (OutStockItem outStockItem : itemList){
            //添加出库时更改商品总数
            Example example = new Example(ProductStock.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("productId",outStockItem.getProductId());
            criteria.andEqualTo("barCode",outStockItem.getBarCode());
            ProductStock productStock = productStockMapper.selectOneByExample(example);
           /* ProductStock productStock = productStockMapper.selectByProductIdAndBarCode(outStockItem.getProductId(),outStockItem.getBarCode());*/
            //判断商品库存是否足够
            if (productStock.getProductNum()-outStockItem.getProductNum()<0){
                Map map = new HashMap();
                map.put("code",20001);
                map.put("msg","商品库存不足");
                return map;
            }
            ProductStock productStock1 = new ProductStock();
            productStock1.setProductNum(outStockItem.getProductNum()-productStock.getProductNum());
            //商品库存足够
            /*Example example1 = new Example(ProductStock.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("productId",outStockItem.getProductId());
            criteria1.andEqualTo("barCode",outStockItem.getBarCode());
            productStockMapper.updateByExampleSelective(productStock,example);*/
            productStockMapper.updateProductNumByProductIdAndBarCode(outStockItem.getProductId(),outStockItem.getBarCode(),productStock.getProductNum()-outStockItem.getProductNum());
        }
        map = new HashMap<String, Object>();
        map.put("outNum", s);
        map.put("dateTime", DateTime);
        return map;
    }

    @Override
    public List<OutStock> listCustomerOutStock(Integer customerId) {
        Example example = new Example(OutStock.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("customerId",customerId);
        example.orderBy("outId").desc();
        return outStockMapper.selectByExample(example);
    }

    @Override
    public List<OutStockItem> outStockDetail(Integer outId) {

        return outStockItemMapper.outStockDetail(outId);
    }

    @Override
    public List<OutStock> outStockListBySeven(Integer userId) {
        return outStockMapper.outStockListBySeven(userId);
    }

    @Override
    public Integer maxId() {
        return outStockMapper.getMaxId();
    }

    @Override
    public PageInfo getOutStock(Integer pageSize, Integer pageNum,Integer companyId) {
        PageHelper.startPage(pageNum, pageSize);
//        List<OutStock> outStocks = outStockMapper.getOutStock();


//        for (OutStock outStock : outStocks) {
//            outStock.setCustomerName(customerMapper.selectByPrimaryKey(outStock.getCustomerId()).getCustomerName());
//            outStock.setUserName(userMapper.selectByPrimaryKey(outStock.getUserId()).getUserName());
//        }
        return new PageInfo(outStockMapper.getOutStock(companyId));
    }

    @Override
    public PageInfo getOutStockItem(Integer pageSize, Integer pageNum, Integer outId) {
        PageHelper.startPage(pageNum, pageSize);
        List<OutStockItem> list = outStockItemMapper.getOutStockItemById(outId);
//        for (OutStockItem outStockItem : list) {
//            //设置商品名称
//            outStockItem.setProductName(productMapper.selectByPrimaryKey(outStockItem.getProductId()).getProductName());
//            //设置商品仓库名称
//            outStockItem.setWarehouseName(warehouseMapper.selectByPrimaryKey(outStockItem.getWarehouseId()).getWarehouseName());
//            //设置商品规格
//            outStockItem.setFormatName(productMapper.selectByPrimaryKey(outStockItem.getProductId()).getFormatName());
//        }
        return new PageInfo(list);
    }

    @Override
    public void applyOutStockItem(Integer outId) {
        OutStock outStock = new OutStock();
        outStock.setApplyStatus(1);
        outStock.setOutId(outId);
        outStockMapper.updateByPrimaryKeySelective(outStock);


    }

    @Override
    public void addOutStockLogistics(Integer outId, String logisticsName, String logisticsNum) {
        OutStock outStock = new OutStock();
        outStock.setOutId(outId);
        outStock.setLogisticsCompany(logisticsName);
        outStock.setLogisticsTracking(logisticsNum);
        outStock.setShippingStatus(1);
        outStockMapper.updateByPrimaryKeySelective(outStock);
    }
}
